//
//  StarredChannelsViewController.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/10.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "StarredChannelsViewController.h"
#import "TAMacro.h"
#import "AFNetworking.h"
#import "GetStarredChannelsOfMySelfTableViewCell.h"

/**
 *  网络请求需要用到的模型
 */
#import "GetStarredChannelsOfSelfRootModel.h"
#import "GetStarredChannelsOfMySelfContent.h"
#import "GetStarredChannelsOfMySelfJoin.h"
#import "ChannelsDetailViewController.h"

@interface StarredChannelsViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *barImageView;
@property (nonatomic, strong) UITableView *starredChannelsTableView;

@property (nonatomic, strong) GetStarredChannelsOfSelfRootModel *getStarredChannelsOfSelfRootModel;


@end

@implementation StarredChannelsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNavigationBar];
    
    self.view.backgroundColor = [UIColor yellowColor];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self netWork];

    
}

- (void)netWork {
    //    c=Search&m=getStarredChannels&p=%7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22selfId%22%3A%2257c01d33a9a36d0621c123f7%22%7D&t=t&v=v105
    //    {"uid":"57c01d33a9a36d0621c123f7","selfId":"57c01d33a9a36d0621c123f7"}
    NSString *userid = @"57c01d33a9a36d0621c123f7";
    NSString *text = [NSString stringWithFormat:@"{\"uid\":\"%@\",\"pid\":\"%@\"}", userid, userid];
    //    NSLog(@"%@", text);
    NSString *utf8Text = [text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
    //    NSLog(@"%@", utf8Text);
    //    NSString *p = [self URLEncodedString:utf8Text];
    //    NSString *parmeters = [NSString stringWithFormat:@"c=Post&m=postDetail&p=%7B%22uid%22%3A%2257c01d33a9a36d0621c123f7%22%2C%22pid%22%3A%225723ebe4a9a36d3340a304b6%22%7D&t=t&v=v105", utf8Text];
    
    NSString *parmeters = [NSString stringWithFormat:@"c=Search&m=getStarredChannels&p=%@&t=t&v=v105", utf8Text];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLString = @"http://api.zheli.io:9501/";
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    // 设置请求参数的字符串, 自定义
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        
        return parmeters;
    }];
    // 正常post发送
    [manager POST:postURLString parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        self.getStarredChannelsOfSelfRootModel = [GetStarredChannelsOfSelfRootModel modelWithDic:responseObject];

        [_starredChannelsTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];
}


- (void)createTableView {
    self.starredChannelsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_starredChannelsTableView];
    _starredChannelsTableView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    _starredChannelsTableView.delegate = self;
    _starredChannelsTableView.dataSource = self;
    [_starredChannelsTableView registerClass:[GetStarredChannelsOfMySelfTableViewCell class] forCellReuseIdentifier:@"GetStarredChannelsOfMySelfTableViewCellReuse"];

    // 去掉多余cell
    [_starredChannelsTableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    // 去掉分割线
    _starredChannelsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _getStarredChannelsOfSelfRootModel.content.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GetStarredChannelsOfMySelfTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GetStarredChannelsOfMySelfTableViewCellReuse"];
    GetStarredChannelsOfMySelfContent *channel = _getStarredChannelsOfSelfRootModel.content[indexPath.row];
    cell.getStarredChannel = channel;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChannelsDetailViewController *channelsDetailVC = [[ChannelsDetailViewController alloc] init];
    GetStarredChannelsOfMySelfContent *channel = _getStarredChannelsOfSelfRootModel.content[indexPath.row];
// 将跳转页面的ID传给push的下一个页面
    channelsDetailVC.channelID = channel.ID;
    [self.navigationController pushViewController: channelsDetailVC animated:YES];
    
    
}

- (void)createNavigationBar {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 27, 30, 30)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backView addSubview:backButton];
    [backButton addTarget:self action:@selector(popViewControllerAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, 42);
    _titleLabel.textAlignment = 1;
    _titleLabel.text = @"我关注的版块";
    _titleLabel.textColor = [UIColor whiteColor];
    [self.backView addSubview:_titleLabel];
    
}

- (void)popViewControllerAction: (UIButton *)backButton {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
